/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "system.h"
#include "HX711.h"
#include "action.h"
#include "key.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
int fputc(int ch,FILE *f)
{
    uint32_t temp = ch;
 
    HAL_UART_Transmit(&huart1,(uint8_t *)&temp,1,0xFFFF);
    HAL_UART_Transmit(&huart2,(uint8_t *)&temp,1,0xFFFF);
    HAL_Delay(10);
 
    return ch;
}
void Printf_Data(void);
void follow_run(void);
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
bool blueState;
float filteredVoltage = 11.1f;
float filteredWeightShiWU = 0;
static const float alpha = 0.10f;
uint8_t firstLinkBlue = 0;
uint8_t enableFollowRun = 0;
uint8_t keyValue = 0;
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for myTask02_led */
osThreadId_t myTask02_ledHandle;
const osThreadAttr_t myTask02_led_attributes = {
  .name = "myTask02_led",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityBelowNormal7,
};
/* Definitions for myTask03_print */
osThreadId_t myTask03_printHandle;
const osThreadAttr_t myTask03_print_attributes = {
  .name = "myTask03_print",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityBelowNormal6,
};
/* Definitions for myTask04 */
osThreadId_t myTask04Handle;
const osThreadAttr_t myTask04_attributes = {
  .name = "myTask04",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityBelowNormal5,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);
void StartTask02(void *argument);
void StartTask03(void *argument);
void StartTask04(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* creation of myTask02_led */
  myTask02_ledHandle = osThreadNew(StartTask02, NULL, &myTask02_led_attributes);

  /* creation of myTask03_print */
  myTask03_printHandle = osThreadNew(StartTask03, NULL, &myTask03_print_attributes);

  /* creation of myTask04 */
  myTask04Handle = osThreadNew(StartTask04, NULL, &myTask04_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
		Get_Weight();
		filteredWeightShiWU = Kalman_Filter(Weight_Shiwu);
		printf("%.2f,%.2f\r\n",filteredWeightShiWU,(float)Weight_Shiwu);
		osDelay(100);	
  }
  /* USER CODE END StartDefaultTask */
}

/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the myTask02_led thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void StartTask02(void *argument)
{
  /* USER CODE BEGIN StartTask02 */
  /* Infinite loop */
  for(;;)
  {
		/*
		keyValue is the return of key which is pressed among 1 to 4
		0 is no key pressed
		*/
		keyValue = key_scan(0);
		osDelay(100);		
  }
  /* USER CODE END StartTask02 */
}

/* USER CODE BEGIN Header_StartTask03 */
/**
* @brief Function implementing the myTask03_print thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask03 */
void StartTask03(void *argument)
{
  /* USER CODE BEGIN StartTask03 */
  /* Infinite loop */
  for(;;)
  {
		HAL_ADC_Start_DMA(&hadc1,(uint32_t*)uhADCxConvertedValue,1);
		VoltageValue[0] = uhADCxConvertedValue[0]*3.3f/4095;
		Voltage = VoltageValue[0] * Voltage_num;
    
    if(Voltage < 11.1f) Voltage = 11.1f;
    if(Voltage > 12.6f) Voltage = 12.6f;    
    
    filteredVoltage = alpha * Voltage + (1 - alpha) * filteredVoltage;
    Voltage_Percent = ((filteredVoltage - 11.1f) / 1.5f) * 100;

		/*
		OLED show:
		set menu and some code
		*/
		#ifdef enableOLED
		OLED_ShowString(0,0,"Bat",16);				OLED_ShowNum(96,0,(int)Voltage_Percent,3,16);		//show battery power percent
		OLED_ShowString(120,0,"%",16);
		

		OLED_ShowString(0,4,"Weight",16);			OLED_ShowNum(96,4,Weight_Shiwu,3,16);						//show Weight_Shiwu
		OLED_ShowString(120,4,"g",16);
		  
		#endif
		if(PBin(12) == 0)
		{
			osDelay(10);
			PBout(13) = 1;
		}
		else
		{
			osDelay(10);
			PBout(13) = 0;
		}
//		Printf_Data();	
    osDelay(100);
  }
  /* USER CODE END StartTask03 */
}

/* USER CODE BEGIN Header_StartTask04 */
/**
* @brief Function implementing the myTask04 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask04 */
void StartTask04(void *argument)
{
  /* USER CODE BEGIN StartTask04 */
  /* Infinite loop */
  for(;;)
  {
		if((Voltage > 16) || (Start_flag == 1))
		{
			Battery_Ready = 1;
			PCout(13) = !PCout(13);
			osDelay(1000);
		}
		else
		{
			Battery_Ready = 0;
			PCout(13) = !PCout(13);
			osDelay(50);
		}
		if(firstLinkBlue == 1 && blueState != 1)		//first time link blue and then linkout blue
		{
			PAout(5) = !PAout(5);
		}
		else if(PBin(3) == 0)
		{
			PAout(5) = !PAout(5);
		}
		else
		{
			PAout(5) = 0;
		}
	}
  /* USER CODE END StartTask04 */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
/* USER CODE BEGIN 4 */

void Printf_Data(void)
{
	
}
void follow_run(void)
{

}
/* USER CODE END 4 */
/* USER CODE END Application */

